package webservices;

import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.GenericEntity;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Request;
import javax.ws.rs.core.UriInfo;

import classes.Korisnik;
import dao.DAOFactory;
@Path("/korisnici")
public class KorisnikResource {
	@Context
	 UriInfo uriInfo;
	 @Context
	 Request request;
	
	 
	 
	 	@Path("/korisnik")
		@GET
		@Produces(MediaType.APPLICATION_XML)
		public Korisnik vratiKorisnika(@HeaderParam("username")String username) {
			try {
				DAOFactory.connect();
				Korisnik k = DAOFactory.getKorisnikDAO().selectKorisnik(username);
				DAOFactory.disconnect();
				return k;
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return null;
			}
		}
	 	
	    @Path("/svi")
		@GET
		@Produces(MediaType.APPLICATION_XML)
		public GenericEntity<List<Korisnik>> vratiKorisnike() {
			try {
				DAOFactory.connect();
				List<Korisnik> korisnici = DAOFactory.getKorisnikDAO().selectSviKorisnici();
				DAOFactory.disconnect();
				return new GenericEntity<List<Korisnik>>(korisnici){};
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return null;
			}
		}
	
	 @PUT
	  @Path("korisnik/novi")
	  @Consumes(MediaType.APPLICATION_XML)
	  @Produces(MediaType.APPLICATION_XML)
	  public String putKorisnik(@HeaderParam("username")String username, @HeaderParam("password")String password, Korisnik k) throws Exception {
		  try{
			  DAOFactory.connect();
			  DAOFactory.getKorisnikDAO().insertKorisnik(k);
			  DAOFactory.disconnect();
			  return Boolean.toString(true);
		  }
		  catch(Exception e){
			  e.printStackTrace();
			  return Boolean.toString(false);
		  }
	  }
	 
	  @POST
	  @Path("korisnik/obrisi")
	  @Consumes(MediaType.APPLICATION_XML)
	  @Produces(MediaType.APPLICATION_XML)
	  public String deleteMarketPOST(@HeaderParam("username")String username, @HeaderParam("password")String password, String ID){
		  try{
			  int IDKorisnik = Integer.parseInt(ID);
			  DAOFactory.connect();
			  int i = DAOFactory.getKorisnikDAO().deleteKorisnik(IDKorisnik);
			  if (i!=1){
				  DAOFactory.disconnect();
				  return Boolean.toString(false);
			  }
			  DAOFactory.disconnect();
			  return Boolean.toString(true);
		  }
		  catch(Exception e){
			  e.printStackTrace();
			  return Boolean.toString(false);
		  }
	  }
		  @POST
		  @Path("korisnik/promijeni")
		  @Consumes(MediaType.APPLICATION_XML)
		  @Produces(MediaType.APPLICATION_XML)
		  public String postKorisnik(@HeaderParam("username")String username, @HeaderParam("password")String password, Korisnik k){
			  try{
				  DAOFactory.connect();
					  int i = DAOFactory.getKorisnikDAO().updateKorisnik(k);
					  if (i!=1){
						  DAOFactory.disconnect();
						  return Boolean.toString(false);
					  }
					  DAOFactory.disconnect();
					  return Boolean.toString(true);
			  }
			  catch (Exception e){
				  e.printStackTrace();
				  return Boolean.toString(false);
			  }
		  }
}
